<?php
	class Banner_Model extends CI_Model {
		function __construct()
		{
			parent::__construct();	
		}
		
		function lists($position_id=0 , $p=1 , $limit=50)
		{
			$start = ($p-1)*50;
			
			$this->db->select('banner.*,banner_position.position ,banner_position.name');
			$this->db->from('banner');
			$this->db->join('banner_position', 'banner.position_id = banner_position.id');
			
			if($position_id!=0){
				$this->db->where('banner.position_id', $position_id); 
			}
			
			$this->db->order_by("date_finish", "desc");
			$this->db->order_by("date_start", "desc");
			$this->db->limit($limit , $start);
			
			$query = $this->db->get();
			return $query->result();
		}
		
		function get_banner_location()
		{
			$this->db->order_by("name", "asc");
			$this->db->from('banner_position');
			$query = $this->db->get();
			return $query->result();
		}
		
		function get_banner($location,$time)
		{
			$this->db->where('position_id',$location); 
			$this->db->where('active',1); 
			$this->db->where('date_start <=',$time); 
			$this->db->where('date_finish >=',$time); 
			$this->db->from('banner');
			$query = $this->db->get();
			return $query->result();
		}
		
		function add($input)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				if($input['location'] != null) {
					$data = array(
						'position_id' => $input['location'] ,
						'url' => $input['url'],
						'code' => $input['code'],
						'used' => $input['used'],
						'date_add' => $input['date_add'],
						'date_start' => $input['date_start'],
						'date_finish' => $input['date_finish'],
						'remark' => $input['remark']
					); 
					$this->db->insert('banner', $data);
					return $this->db->insert_id();
				}else{
					return 0;
				}
			}else{
				return 0;
			}
		}
		
		function update($input)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				if($input['id'] != null) {
					$data = array(
						'position_id' => $input['location'] ,
						'url' => $input['url'],
						'code' => $input['code'],
						'used' => $input['used'],
						'date_modify' => $input['date_modify'],
						'date_start' => $input['date_start'],
						'date_finish' => $input['date_finish'],
						'remark' => $input['remark']
					); 
					$this->db->where('id', $input['id']);
					$this->db->update('banner', $data);
					return $this->db->affected_rows();
				}else{
					return 0;
				}
			}else{
				return 0;
			}
		}
		
		function update_img($input)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				if($input['id'] != null) {
					$data = array(
						'img' => $input['img'] ,
					);
					$this->db->where('id', $input['id']);
					$this->db->update('banner', $data);
					return $this->db->affected_rows();
				}else{
					return 0;
				}
			}else{
				return 0;
			}
		}
		
		function status($id)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				$query = $this->get_banner_info($id);
				
				$data = array(
					'active' => ABS($query[0]->active - 1) ,
				);
				$this->db->where('id', $id);
				$this->db->update('banner', $data);
			}
		}
		
		function delete($id)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				$this->db->where('id', $id); 
				return $this->db->delete('banner');
			}
		}
		
		function get_banner_location_info($id)
		{
			$this->db->where('id', $id);
			$this->db->from('banner_position');
			$query = $this->db->get();
			return $query->result();
		}
		
		function get_position_name($id)
		{
			$result = $this->get_banner_location_info($id);
			return $result[0]->name;
		}
		
		function get_position($id)
		{
			$result = $this->get_banner_location_info($id);
			return $result[0]->position;
		}
		
		function get_banner_info($id)
		{
			$this->db->where('id', $id);
			$this->db->from('banner');
			$query = $this->db->get();
			return $query->result();
		}
		
		function get_banner_img($id)
		{
			$query = $this->get_banner_info($id);
			return $query[0]->img;
		}
		
		function get_banner_message()
		{
			$this->db->from('text_message');
			$this->db->where('id',1);
			$query = $this->db->get();
			$result = $query->result();
			return $result[0]->detail;
		}
		
		function update_banner_message($str)
		{
			$data = array(
				'detail' => $str ,
			);
			$this->db->where('id', 1);
			$this->db->update('text_message', $data);
			return $this->db->affected_rows();
		}
	}


/* End of file admin_model.php */
/* Location: ./application/models/admin/admin_model.php */